﻿using System.Timers;

namespace Deflection
{
    class CustomTimer
    {
        Timer timer = new Timer();

        private int counter = 0;

        /// <summary>
        /// The current counter / number of ticks
        /// </summary>
        public int Counter
        {
            get { return counter; }
            set { counter = value; }
        }

        private int interval = 1000;

        /// <summary>
        /// The time in milliseconds between ticks
        /// </summary>
        public int Interval
        {
            get { return interval; }
            set { interval = value; }
        }

        /// <summary>
        /// The state of the timer
        /// </summary>
        public bool Enabled
        {
            get { return timer.Enabled; }
            set { timer.Enabled = value; }
        }

        /// <summary>
        /// Default interval/tick is 1 second (1000 milliseconds)
        /// </summary>
        public CustomTimer()
        {
            this.timer.Interval = this.interval;
            this.timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
        }

        /// <summary>
        /// Overload constructor incase you want an interval other than 1 second
        /// </summary>
        /// <param name="interval">The interval between ticks in milliseconds</param>
        public CustomTimer(int interval)
        {
            this.interval = interval;

            this.timer.Interval = this.interval;
            this.timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);           
        }

        /// <summary>
        /// Event called on each tick of the timer
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            // Increase the counter by one, 1 tick
            Counter++;
        }

        /// <summary>
        /// Start the timer
        /// </summary>
        public void Start()
        {
            timer.Start();
        }

        /// <summary>
        /// Stop the timer
        /// </summary>
        public void Stop()
        {
            timer.Stop();
        }

        /// <summary>
        /// Resets the timer
        /// </summary>
        public void Reset()
        {
            Counter = 0;
        }

        /// <summary>
        ///  Restarts the timer
        /// </summary>
        public void Restart()
        {
            Stop();
            Reset();
            Start();        
        }

        


    }
}
